package cn.edu.zafu.bigdata.lmr;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.*;

class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String id;    // 学号
    private String name;  // 姓名
    private String phone; // 电话

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    // Getter 和 Setter 方法
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    // 重写 toString() 方法
    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

class StudentFileManager {
    private static final String FILE_NAME = "students.dat";

    public static List<Student> loadStudents() {
        List<Student> students = new ArrayList<>();
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            students = (List<Student>) ois.readObject();
        } catch (FileNotFoundException e) {
            // 文件不存在，可能是第一次运行程序，忽略这个异常
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return students;
    }

    public static void saveStudents(List<Student> students) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class StudentManagementSystem {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        List<Student> students = StudentFileManager.loadStudents();

        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // consume newline

            switch (choice) {
                case 1:
                    addStudent(students);
                    break;
                case 2:
                    searchStudent(students);
                    break;
                case 3:
                    updateStudent(students);
                    break;
                case 4:
                    deleteStudent(students);
                    break;
                case 5:
                    StudentFileManager.saveStudents(students);
                    System.out.println("退出程序。");
                    System.exit(0);
                default:
                    System.out.println("未找到该学生");
            }
        }
    }

    private static void addStudent(List<Student> students) {
        System.out.print("输入学生学号: ");
        String id = scanner.nextLine();
        if (isIdExists(students, id)) {
            System.out.println("未找到该学生");
            return;
        }
        System.out.print("输入学生姓名: ");
        String name = scanner.nextLine();
        System.out.print("输入学生电话: ");
        String phone = scanner.nextLine();

        Student student = new Student(id, name, phone);
        students.add(student);
        System.out.println("学生信息添加成功！");
    }

    private static void searchStudent(List<Student> students) {
        while (true) {
            System.out.print("输入学生学号进行检索 (输入'QUIT'退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            for (Student student : students) {
                if (student.getId().equals(id)) {
                    System.out.println(student);
                    return;
                }
            }
            System.out.println("未找到该学生");
        }
    }

    private static void updateStudent(List<Student> students) {
        while (true) {
            System.out.print("输入学生学号进行更新 (输入'QUIT'退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            for (Student student : students) {
                if (student.getId().equals(id)) {
                    System.out.print("输入新的学生姓名: ");
                    String name = scanner.nextLine();
                    System.out.print("输入新的学生电话: ");
                    String phone = scanner.nextLine();

                    student.setName(name);
                    student.setPhone(phone);
                    System.out.println("学生信息更新成功！");
                    return;
                }
            }
            System.out.println("未找到该学生");
        }
    }

    private static void deleteStudent(List<Student> students) {
        while (true) {
            System.out.print("输入学生学号进行删除 (输入'QUIT'退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            students.removeIf(student -> student.getId().equals(id));
            System.out.println("学生信息删除成功！");
        }
    }

    private static boolean isIdExists(List<Student> students, String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }
}
